package com.th.supcom.pds.dispensedrug.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.auditprescription.client.model.PdsPrescDetailCombineModel;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.dispensedrug.client.service.DispenseDrugRpc;
import com.th.supcom.pds.domain.pojo.PdsPrescDetailCombine;
import com.th.supcom.pds.domain.pojo.PdsSupply;
import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.domain.vo.PdsSupplyVO;
import com.th.supcom.pds.domain.vo.Result;
import com.th.supcom.pds.internal.api.DispenseDrugService;
import com.th.supcom.pds.internal.api.PdsSupplyDetailService;
import com.th.supcom.pds.internal.api.PdsSupplyService;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.model.ResultModel;

/**
 * 核对发药控制层.
 * 
 * @author 杨冲
 */
public class DispenseDrugServiceController extends GwtController implements DispenseDrugRpc
{

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private static final long serialVersionUID = -7252559535751128820L;
    /**
     * 日志记录器.
     */
    public static final Logger s_logger = LoggerFactory.getLogger (DispenseDrugServiceController.class);
    private PdsSupplyService m_pdsSupplyService;
    private PdsSupplyDetailService m_pdsSupplyDetailService;
    private DispenseDrugService m_dispenseDrugService;

    /**
     * 默认构造函数.
     */
    public DispenseDrugServiceController ()
    {

    }

    public void setPdsSupplyService (PdsSupplyService pdsSupplyService)
    {
        m_pdsSupplyService = pdsSupplyService;
    }

    public void setPdsSupplyDetailService (PdsSupplyDetailService pdsSupplyDetailService)
    {
        m_pdsSupplyDetailService = pdsSupplyDetailService;
    }

    public void setDispenseDrugService (DispenseDrugService dispenseDrugService)
    {
        m_dispenseDrugService = dispenseDrugService;
    }

    @Override
    public void dispenseDrug (final List <PdsSupplyDetailModel> models, final String operator, String deptCode, String patientId) throws UiException
    {
        s_logger.info ("调用dispenseDrug处方发药，参数信息：pdsSupplyDetailModel:{},operator:{},deptCode:{}", new Object[]
        { models, operator, deptCode });
        final List <PdsSupplyDetailVO> pdsSupplyDetails = ConvertUtils.convert (PdsSupplyDetailVO.class, models);
        m_dispenseDrugService.dispenseDrug (pdsSupplyDetails, operator, deptCode, patientId);
    }

    @Override
    public List <PdsPrescDetailCombineModel> getPhamBatchByExample (final PdsPrescDetailCombineModel model) throws UiException
    {
        final PdsPrescDetailCombine pdsPrescDetailCombine = ConvertUtils.convert (PdsPrescDetailCombine.class, model);
        final List <PdsPrescDetailCombine> pdsPrescDetailCombines = null;
        // m_dispenseDrugService.getPhamBatchByExample (pdsPrescDetailCombine);
        return ConvertUtils.convert (PdsPrescDetailCombineModel.class, pdsPrescDetailCombines);
    }

    @Override
    public List <PdsSupplyModel> getPdsSupplyListByExample (PdsSupplyModel pdsSupplyModel) throws UiException
    {
        s_logger.info ("调用getPdsSupplyListByExample查询供药信息列表,参数信息,pdsSupplyModel:{}", new Object[]
        { pdsSupplyModel });
        PdsSupply pdsSupply = ConvertUtils.convert (PdsSupply.class, pdsSupplyModel);
        List <PdsSupply> list = m_pdsSupplyService.findPdsSupplyListByExample (pdsSupply);
        return ConvertUtils.convert (PdsSupplyModel.class, list);
    }

    @Override
    public ResultModel getPdsSupplyDetailListByRcptNo (String rcptNo, String prescNo, String deptCode) throws UiException
    {
        s_logger.info ("调用getPdsSupplyDetailListByRcptNo查询供药信息列表,参数信息:rcptNo:{},prescNo:{},deptCode:{}", new Object[]
        { rcptNo, prescNo, deptCode });
        Map <String, Object> map = new HashMap <String, Object> ();
        map.put ("rcptNo", rcptNo);
        map.put ("prescNo", prescNo);
        map.put ("performedBy", deptCode);
        Result result = m_pdsSupplyDetailService.findPdsSupplyDetailByRcptNo (map);
        if(null == result) {
            return null;
        }
        PatientInfoModel patientInfoModel = ConvertUtils.convert (PatientInfoModel.class, result.getPatientInfo ());
        List <PdsSupplyDetailModel> pdsSupplyDetailModels = ConvertUtils.convert (PdsSupplyDetailModel.class, result.getPdsSupplyDetailVos ());
        ResultModel resultModel = new ResultModel ();
        resultModel.setPatientInfoModel (patientInfoModel);
        resultModel.setPdsSupplyDetailModels (pdsSupplyDetailModels);
        return resultModel;
    }

    @Override
    public List <PdsSupplyModel> findPdsSupplyListWithTotalPrice (String deptCode, String ipAdress) throws UiException
    {
        s_logger.info ("调用findPdsSupplyListWithTotalPrice方法查询供药信息,参数信息:deptCode:{},ipAdress:{}", new Object[]
        { deptCode, ipAdress });
        Map <String, Object> map = new HashMap <String, Object> ();
        map.put ("ipAdress", ipAdress);
        map.put ("performedBy", deptCode);

        List <PdsSupplyVO> list = m_pdsSupplyService.findPdsSupplyListWithTotalPrice (map);
        return ConvertUtils.convert (PdsSupplyModel.class, list);
    }
}